package cn.itsource.dept.controller;

import cn.itsource.dept.service.IDepartmentService;
import cn.itsource.dept.domain.Department;
import cn.itsource.dept.query.DepartmentQuery;

import cn.itsource.utills.AjaxResult;
import cn.itsource.utills.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    public IDepartmentService departmentService;


    /**
     * 保存和修改公用的
     * @param department  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if( department.getId()!=null)
                departmentService.update(department);
            else
                departmentService.add(department);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setSuccess(false).setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                departmentService.delete(id);
            return  AjaxResult.success().setSuccess(true).setMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setSuccess(false).setMsg("操作失败");
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {
            Department findnoe = departmentService.findnoe(id);
            return AjaxResult.success().setData(findnoe);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Department> list(){

        return departmentService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pagelist")
    public AjaxResult findQuey(@RequestBody DepartmentQuery query)
    {
        try {
            List<Department> queryData = departmentService.queryData(query);
            Long count = departmentService.queryCount(query);
            return AjaxResult.success().setData(queryData).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
    @GetMapping("/tree")
    @ApiOperation(value = "返回部门树")
    public List<Department> getDeptTree(){
        return departmentService.deptTree();
    }
    /*导出*/
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Department> all = departmentService.findAll();
        ExcelUtil.exportExcel(all,"部门数据title","部门数据",Department.class,"导出的部门数据.xls",response);
    }
    /*导入*/
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Department> shops = ExcelUtil.importExcel(file, 1, 1, Department.class);
        //做数据校验
        shops.forEach(System.out::println);
    }
}
